package com.lantosec.hmrp.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.system.entity.SysDeptPosition;
import com.lantosec.hmrp.modules.system.entity.SysPosition;
import com.lantosec.hmrp.modules.system.entity.SysPositionRole;
import com.lantosec.hmrp.modules.system.mapper.SysPositionMapper;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import com.lantosec.hmrp.modules.system.service.ISysPositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-12
 */
@Service
@Primary
@Log4j2
public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper, SysPosition> implements ISysPositionService {

    /**
     * 更新岗位管理排序
     * @param id
     * @param positionOrder
     */
    @Override
    public void updateOrder(String id, Integer positionOrder) {
        SysPosition sysPosition = new SysPosition();
        sysPosition.setPositionOrder(positionOrder);
        baseMapper.update(sysPosition,new QueryWrapper<SysPosition>().eq(SysPosition.ColName.position_id.toString(),id));
    }

    @Override
    public PageVO<SysDeptPosition> getPositionByDeptId(PageVO<SysDeptPosition> page, String deptId) {
        //查询部门下的岗位信息
        PageVO<SysDeptPosition> sysDeptPositionList = baseMapper.getPositionByDeptId(page, deptId);
        List<SysDeptPosition> sysDeptPositions = sysDeptPositionList.getRecords();
        sysDeptPositions.forEach(param->{
            String deptName = param.getDeptName();
            String positionName = param.getPositionName();
            param.setPositionName(deptName+"-"+positionName);
        });
        sysDeptPositionList.setRecords(sysDeptPositions);
        return sysDeptPositionList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysDeptPosition(SysDeptPosition sysDeptPosition) {
        //查询岗位的部门
        String parentPosition = sysDeptPosition.getParentPosition();
        String parentPositionDept = sysDeptPosition.getParentPositionDept();
        //更新部门岗位的上级管理岗位
        baseMapper.updateSysDeptPosition(sysDeptPosition);
        //设置被设为上级管理岗位的部门岗位为管理岗 manager_post=1
        baseMapper.updateSysDeptPositionManager(parentPosition,parentPositionDept);
    }

    @Override
    public List<SysDeptPosition> getPositionByDeptIdList(String deptId) {
        return baseMapper.getPositionByDeptIdList(deptId);
    }

    @Override
    public List<SysPositionRole> getSysPositionRole(String deptId, String positionId) {
        return baseMapper.getSysPositionRole(deptId,positionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysPositionRole(SysPositionRole sysPositionRole) {
        String deptId = sysPositionRole.getDeptId();
        String positionId = sysPositionRole.getPositionId();
        String roleId = sysPositionRole.getRoleId();
        //修改部门岗位角色，先删后插
        baseMapper.deleteSysPositionRole(deptId,positionId);
        String[] roleIds = roleId.split(",");
        for(int i=0;i<roleIds.length;i++){
            baseMapper.saveSysPositionRole(deptId,positionId,roleIds[i]);
        }
    }

    @Override
    public Result deletePositionInfo(String ids) {
        //查询该岗位是否已有关联
        //查询是否已存在在部门岗位挂接表
        List<SysDeptPosition> sysDeptPositions = baseMapper.selectSysDeptPosition(ids);
        if(CollectionUtils.isNotEmpty(sysDeptPositions)){
            return Result.ok("该岗位已存在关联信息，不能删除",false);
        }
        //查询是否已存在部门人员挂接表
        List<SysDeptPosition> sysDeptUsers = baseMapper.selectSysDeptUser(ids);
        if(CollectionUtils.isNotEmpty(sysDeptUsers)){
            return Result.ok("该岗位已存在关联信息，不能删除",false);
        }else {
            this.removeById(ids);
            return Result.ok();
        }
    }
}
